/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.*;
import java.util.List;

/**
 *
 * @author Dine
 */
public class CheckDAO extends DAO {

    public CheckDAO() {
        super();
    }
    //BASIC DAO METHODS

    @SuppressWarnings("unchecked")
    public List<Checks> getAll() throws Exception {
        List<Checks> a = (List<Checks>) getSession().createQuery("from Checks a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Checks get(int id) throws Exception {
        Checks a = (Checks) getSession().createQuery("from Checks a where a.checkid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Checks a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Checks a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }
    // OTHER METHODS

    public List<Checks> getCCRChecks(String date, int branchid) throws Exception {
        // System.out.println(branchid);

        List<Checks> list = getSession().createQuery("from Checks c where c.checkdate=:date and c.loans.areas.branches.branchid=:branchid").setParameter("date", date).setParameter("branchid", branchid).setCacheable(true).list();
        //list = session.createQuery("from Checks c where c.checkdate=:date").setParameter("date", date).list();
        flush();
        //System.out.println("ccrcoll:" + list);
        return list;

    }
}
